GPS Map

By Rohan Varma

This tutorial will walk you through displaying a user’s current location on a google map. It will utilize the Google Maps activity option on AndroidStudio.

 

  1. Make new project

·       Start a new android studio project. In this example, my project will be called MapTutorial and will have the package tutorialhttps://lh6.googleusercontent.com/O6YPTpBykxjX2ee1G_mch2lrfszPkHRfbRxddKvjeWU9KZgqrYscYNjkyebgEeN1lgCY2dun0fXByb5H4_Gm6FgkGzOSMRcIcwHb2HukAQ2ui8kyY7RGsnB6pjfb0_OPlQpzzuR3MiLoBQLy

 

·       On the Activity selection screen choose Google Maps Activity and then create the project

https://lh4.googleusercontent.com/_NNxrp5shpPRIxQxLpP3g-x9LGWj_AXCBg7-BXzSnBF-YBx4hIb3y7aiIE3QQYgiGCbZEpXdIoQ-xqvsRXVOa3-p94Q_0-GzrsaVGF56lxuX3DzKVss0MgwlCYkbTbFysDGqDEx1VQ45CB1T

2.              Get Developer Key

·       When your project is created, Android Studio will automatically open google_maps_api.xml, telling you that you need a Google Maps API key. It will also give you a link  to follow in order to get this key

https://lh4.googleusercontent.com/Or-BgWUTn0HkyMPiLk5BQjcTCwBS8DHKV4c6TF5nq1pB1vo8iI9FontQgqCioXYIL0_g_axgqB0SELUXQ_OLom_b03QSvyx0FdTjZXTQ_HFKqxMFTtMtk7oTMWXH5WnzksTW_LpcX2GPrvLo

·       Paste that link into your browser and it should take you to a page that looks like this

https://lh3.googleusercontent.com/SfRJcZyQA9d8dNO-etEyolvpM1hPJ6VMvDifd36_fypsNat_536WpyypJgFmoChp3Rawnr4ujz8k2dr6nczrTpeNeLpXjBWviHLTSJNND-zLIrEZuvSojNLLbTjcqJp_xuIw2isDKOQZMEma

If Create a new project is not selected, select it from the drop down menu and then click continue

·       A little bit after clicking continue, you will be redirected to a page saying that the API is enabled but to use the API you’ll need the right credentials. Click the blue button that says Go to Credentials

 

·       You will then be redirected to a page that looks like thishttps://lh4.googleusercontent.com/GJtQBVOwyOhakA1BKBFARbnHGLnA5CTj7VZBlVuHclaBb4vKeiV9JJK9MW3xRfMIIiec3pIx9koWx6ttEywYCcdUN1veUNTppjW1KqVxvaCn-EGp6k7LnkbrgmKu27ETuBwdyelQ4ubKqcJU















It is possible that the box is already auto filled. If it is, go back to your google_maps_api.xml and check that whatever is in the box matches the credentials the xml file provides. If not, copy the credentials from the xml file into the box.https://lh6.googleusercontent.com/5oLYazhsVkInkgpzpTO1tit8NX82n4zr3FcpCBCdbunwdsATsL32q2Alufj6JgfXY_TUx1rgtDzaqLTOhqj2wFKZjdvUWVEr4cXIAjXRpMHP-8O53Qdi8vXlTdHKKTMReQ6Eh8noCmnl1TH1




·       After filling in the box, click Create and you should be redirected to a page that gives you an API keyhttps://lh5.googleusercontent.com/Xz16W0c_Fgs7EvpZhGNxa_eKKBvik5mOn50LKvRdeDzuRAy96qz31O7afvPvpQI1yEz4nY0nxOvi1_JKV4gasrLBN31D3gBqvTADbWYGxWCS5U_Xb_ZBdMoJqbFBFByAYoGzik28bVEe-o8F




Copy the highlighted section and paste it into your google_maps_api.xml file (delete the part where it says YOUR KEY HERE and replace it with the highlighted section)


https://lh4.googleusercontent.com/Lbi49qraqBUsEvibon3qtk1Od2zuw1_NeEYhLUryrI6cHxe6ZkpyRRcgMDiB_d0SEl5HYdGvzb-t3qL0WFYyXFCju-jeVwzOG2zflQTRUW9QytYmTQlPFp0YeI0PB0pvHY5EBfAz0MPrNyOv

·       Once you do this, you are ready to begin programming. However, in order to ensure that everything is working properly, run your program. If it displays a map with a marker somewhere along the equator, everything is running properly. Otherwise, go back and check if you missed one of the steps. Note that it will probably take Gradle a considerable amount of time to build the project the first time.

3.              Display user’s current location

·       Displaying a user’s location is fairly easy as all you need to do is add the following code to your onMapReady() method in MapsActivity.java. You should also delete the built in marker.

 

 

 

public void onMapReady(GoogleMap googleMap) {
   
mMap = googleMap;

   
if (mMap != null) {
       
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.i(
"info", "onCreate: no permis");
            finish();
           
return;
        }
       
mMap.setMyLocationEnabled(true);
    }
}

 

 

 

·       Now when you open the application, there should be a blue dot at your location. If you move around, the blue dot should move and show a small blue chevron indicating the direction in which you are currently moving. Note that the blue dot will periodically update its location, so don’t be surprised if updates are kind of choppy. Additionally, at the top right corner of the screen, a button will appear that centers the map on your current location.